package com.coo.stage
{
	import com.coo.events.StageEvent;
	
	import flash.display.*;
	import flash.events.*;

	public class StageManager extends EventDispatcher
	{
		private static var _instance: StageManager;
		
		private var _observers: Array;
		private var _stage: Stage;
		
		public function StageManager( enforcer: SingletonEnforcer ){}
		
		public static function getInstance(): StageManager
		{
			if ( _instance == null ) _instance = new StageManager( new SingletonEnforcer );
			return _instance;
		}
		
		public function activate( stage: Stage, align: String = "TL" ): void
		{
			_stage 										= stage;
			_stage.align 								= align;
			_stage.scaleMode 						= StageScaleMode.NO_SCALE;
			_stage.showDefaultContextMenu 	= false;
		}
		
		public function addResizeListener(): void
		{
			_observers = new Array();
			_stage.addEventListener( Event.RESIZE, broadcast );
		}
		
		public function broadcast( e: Event = null ): void
		{
			dispatchEvent( new StageEvent( StageEvent.RESIZE, _stage.stageWidth, _stage.stageHeight ));
		}
		
		public function insert( o: IStageObserver ):void
		{
			if( _observers.indexOf( o ) === -1 && o is DisplayObject ) add( o );
		}
		
		private function add( o: IStageObserver ): void
		{
			_observers.push( o );
			addEventListener( StageEvent.RESIZE, o.setPosition );
		}
		
		public function remove( o: IStageObserver ): void
		{
			removeEventListener( StageEvent.RESIZE, o.setPosition );
			
			var index: int = _observers.indexOf( o );
			_observers.splice( index, 1 );
		}
		
		public function get observerList(): Array
		{
			return _observers;
		}
//---- EVENT HANDLERS ------------------------------------------------------------------------------------
//---- STATIC METHODS ----------------------------------------------------------------------------
//---- GETTERS / SETTERS -------------------------------------------------------------------------
	}
}

class SingletonEnforcer {}